package com.controller;

import com.domain.Blog;
import com.domain.Comment;
import com.service.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentServiceImpl commentService;

    //向comment页面跳转
    @GetMapping("/comment")
    public String comment(){
        return "redirect:showComment";
    }

    //send
    @ResponseBody
    @PostMapping(value = "/sendComment")
    public String sendComment(String nike,String email,String message,int bid){
        Comment comment = new Comment();
        comment.setNike(nike);
        comment.setEmail(email);
        comment.setContent(message);
        String sendtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        comment.setSendtime(sendtime);
        Blog blog = new Blog();
        blog.setId(bid);
        comment.setBlog(blog);
        int flag = commentService.sendComment(comment);
        if (flag > 0){
            return "success";
        }
        return "error";
    }

    //展示comments
    @GetMapping("/showComment")
    public ModelAndView showComment(){
        ModelAndView modelAndView = new ModelAndView("template/comment");
        List<Comment> commentList = commentService.findAllComment();
        modelAndView.addObject("commentList",commentList);
        return modelAndView;
    }

    //删除comment
    @GetMapping("/deleteComment")
    public String deleteComment(String id){
        commentService.deleteComment(Integer.parseInt(id));
        return "redirect:comment";
    }

}
